package com.gewu.pm.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.dialog.TipsTwoDialog;

/**
 * 确认订单页面
 */
public final class ConfirmOrderActivity extends AppActivity {

    private TextView mTerminalNameView;
    private TextView mTerminalCodeView;
    private TextView mOrderDateView;
    private TextView mDeliveryDateView;
    private TextView mVehicleCodeView;
    private TextView mDeliveryMerchantView;
    private TextView mSalespersonView;
    private TextView mPromotionTypeView;
    private TextView mPromotionNameView;
    private TextView mTotalQuantityView;
    private TextView mTotalAmountView;
    private TextView mDiscountAmountView;
    private TextView mOrderAmountView;
    private TextView mSubmitButton;

    /**
     * 合计数量字符串
     */
    private String mTotalQuantityText;
    /**
     * 合计金额
     */
    private double mTotalAmount;

    /**
     * 启动确认订单页面
     *
     * @param context       上下文
     * @param totalQuantity 合计数量（如：3包4罐2瓶）
     * @param totalAmount   合计金额
     */
    public static void start(Context context, String totalQuantity, double totalAmount) {
        Intent intent = new Intent(context, ConfirmOrderActivity.class);
        intent.putExtra(IntentKey.TITLE, totalQuantity);
        intent.putExtra(IntentKey.BALANCE, String.valueOf(totalAmount));
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_confirm_order;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTerminalNameView = findViewById(R.id.tv_title);
        mTerminalCodeView = findViewById(R.id.tv_terminal_code);
        mOrderDateView = findViewById(R.id.tv_order_date);
        mDeliveryDateView = findViewById(R.id.tv_delivery_date);
        mVehicleCodeView = findViewById(R.id.tv_vehicle_code);
        mDeliveryMerchantView = findViewById(R.id.tv_delivery_merchant);
        mSalespersonView = findViewById(R.id.tv_salesperson);
        mPromotionTypeView = findViewById(R.id.tv_promotion_type);
        mPromotionNameView = findViewById(R.id.tv_promotion_name);
        mTotalQuantityView = findViewById(R.id.tv_total_quantity);
        mTotalAmountView = findViewById(R.id.tv_total_amount);
        mDiscountAmountView = findViewById(R.id.tv_discount_amount);
        mOrderAmountView = findViewById(R.id.tv_order_amount);
        mSubmitButton = findViewById(R.id.btn_submit);

        // 设置点击监听
        setOnClickListener(mSubmitButton);
    }

    @Override
    protected void initData() {
        // 获取 Intent 参数
        mTotalQuantityText = getString(IntentKey.TITLE);
        String totalAmountStr = getString(IntentKey.BALANCE);

        if (totalAmountStr != null) {
            try {
                mTotalAmount = Double.parseDouble(totalAmountStr);
            } catch (NumberFormatException e) {
                mTotalAmount = 0;
            }
        }

        // 加载模拟数据
        loadMockData();

        // 设置合计信息
        if (mTotalQuantityText != null) {
            mTotalQuantityView.setText(mTotalQuantityText);
        }

        mTotalAmountView.setText(String.format("¥%.2f", mTotalAmount));

        // 计算订单金额（合计金额 - 优惠）
        double discountAmount = 20.00;  // 测试优惠金额
        double orderAmount = mTotalAmount - discountAmount;

        mDiscountAmountView.setText(String.format("¥%.2f", discountAmount));
        mOrderAmountView.setText(String.format("¥%.2f", orderAmount));
    }

    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        mTerminalNameView.setText("终端名称展示在这里");
        mTerminalCodeView.setText("POS00011718");
        mOrderDateView.setText("___");
        mDeliveryDateView.setText("2025-8-18");
        mVehicleCodeView.setText("A12333");
        mDeliveryMerchantView.setText("小配送56号车");
        mSalespersonView.setText("霍辉");
        mPromotionTypeView.setText("买赠活动；满减活动");
        mPromotionNameView.setText("购买xxx商品1箱，赠送xxx商品1盒；满50减10块；");
    }

    @SingleClick
    @Override
    public void onClick(android.view.View view) {
        if (view.getId() == R.id.btn_submit) {
            new TipsTwoDialog.Builder(getContext()).setTitle("您确定要提交订单吗").setListener(dialog -> {
                // 提交订单
                submitOrder();
            }).show();

        }
    }

    /**
     * 提交订单
     */
    private void submitOrder() {
        // TODO: 调用API提交订单
        toast("提交订单成功");
        finish();
    }
}

